app.controller("addupdateSale",function($scope,$http, $stateParams,get,post,$rootScope,Md5,$state){
	$scope.sexList=[{id:-1,name:"请选择"},{id:0,name:"是"},{id:1,name:'否'}]
	$scope.id=$stateParams.id
	$scope.selectss=function(){
		//项目名称
		get.list("dictionary/project").then(function(e){
				$scope.project = e
				$scope.project.unshift({
						diName:"请选择",
						diId:-1
					})
			})
		
		//合同甲方
		get.list("dictionary/saleCompany").then(function(e){
				$scope.saleCompany = e
				$scope.saleCompany.unshift({
						diName:"请选择",
						diId:-1
					})
				
			})
		
		//合同类型
		get.list("dictionary/contactTypeDic").then(function(e){
				$scope.contactTypeDic = e
					$scope.contactTypeDic.unshift({
						diName:"请选择",
						diId:-1
					})
		})
		//收款计划
		get.list("dictionary/paymentDic").then(function(e){
				$scope.paymentDic = e
					$scope.paymentDic.unshift({
						diName:"请选择",
						diId:-1
					})
		})
		$scope.projectId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
		
	}
	$scope.selectss();
	$scope.isshows=false
	$scope.add={};
	setTimeout(function (){
	if($stateParams.id!=''){
		get.list('sale/findOne',{cId:$stateParams.id}).then(function(e){
							$scope.pickertime=e.cSignTime
							$scope.add.cId=e.cId
							$scope.add.projectId=e.projectId
							$scope.add.cSerialNum=e.cSerialNum
							$scope.add.cName=e.cName
							$scope.add.cPecific=e.cPecific
							$scope.add.cAmount=e.cAmount||''
							$scope.add.cForecost=e.cForecost||''
							$scope.add.cCycle=e.cCycle||''
							$scope.add.cKind=e.cKind
							$scope.add.cPaymentPlan=e.cPaymentPlan
							$scope.add.cIfCompletePlan=e.cIfCompletePlan
							$scope.add.dateTime=e.SignTime
							$scope.add.cIfPayInvoice=e.cIfPayInvoice
							$scope.add.cRemark=e.cRemark||''
							$scope.add.payAcount=e.payAcount||''
							$scope.add.receivables=e.receivables||''
							$scope.add.companyPecificId=e.companyPecificId
					if($scope.add.companyPecificId==null){
						//项目类型
						$scope.companyPecificId={
									value:{
										diName:"请选择",
										diId:null
									}
						}
					}else{
						//合同甲方
						angular.forEach($scope.saleCompany,function(o){
							if(e.companyPecificId==o.diId){
								$scope.companyPecificId={
									value:o
								}
							}
						})
					}
					//项目名称
					angular.forEach($scope.project,function(o){
						if(e.projectId==o.diId){
							$scope.projectId={
								value:o
							}
						}
					})
					
					
		})
	}else{
		//项目名称
			
			//项目类型
			$scope.companyPecificId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
		$scope.add.pSerialNum="WXKJXM"+new Date().format("yyyyMMdd")
			$scope.add.cPaymentPlan=-1
			$scope.add.cIfCompletePlan=-1
			$scope.add.cIfPayInvoice=-1
			$scope.add.cKind=-1
		
	}
	},200)
	$scope.$watch('add.projectId', function (value) {
		if(null!=value&&null!=value.diId){
			get.list("sale/findlike",{cSerialNum:value.dgDecription}).then(function(e){
				if(e>0){
					$scope.add.cSerialNum=value.dgDecription+"-0"+e	
				}else{
					$scope.add.cSerialNum=value.dgDecription
				}
				$scope.add.cName=value.diName
			})
		
		}
	})
	//取消
	$scope.rement=function(){
		$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
	}
	$scope.sub=function(val){
		$scope.add.cType=26
		if($scope.add.projectId!=null){
			$scope.add.projectId=$scope.add.projectId.diId||$scope.add.projectId
		}
		if($scope.add.companyPecificId!=null){
			var comid=$scope.add.companyPecificId.diId||$scope.add.companyPecificId
			var comName=$scope.add.companyPecificId.diName||$scope.add.cPecific
			$scope.add.companyPecificId=comid
			if(comName!="请选择"){
				$scope.add.cPecific=comName
			}else{
				$scope.add.cPecific=''
			}
			
		}
		$scope.add.dateTime=$scope.pickertime||$scope.add.dateTime
			if($stateParams.id!=''){
				$scope.add.pId=$stateParams.id
				post.list("sale/update",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
			}else{
				
				post.list("sale/add",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
				
			}
			
			
		
		
	}
})
